%%%--------------------------------------
%%% @Module  : pt_39
%%% @Author  : Xianrong.Mai
%%% @Created : 2010.04.09
%%% @Description: 神岛空战的解包和组包
%%%--------------------------------------
-module(pt_39).

%%
%% Include files
%%
-include("common.hrl").
%%
%% Exported Functions
%%
-export([read/2, write/2]).

%%
%% API Functions
%%
%%%=========================================================================
%%% 解包函数
%%%=========================================================================
%% -----------------------------------------------------------------
%% 39001 查看已报名氏族
%% -----------------------------------------------------------------
read(39001, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39002 报名空岛神战
%% -----------------------------------------------------------------
read(39002, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39003 进入空岛神战
%% -----------------------------------------------------------------
read(39003, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39004 离开空岛神战
%% -----------------------------------------------------------------
read(39004, <<Type:8>>) ->
	{ok, [Type]};

%% -----------------------------------------------------------------
%% 39007 战场信息
%% -----------------------------------------------------------------
read(39007, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39008 开旗
%% -----------------------------------------------------------------
read(39008, <<Area:8>>) ->
	{ok, [Area]};

%% -----------------------------------------------------------------
%% 39012 拾取战旗
%% -----------------------------------------------------------------
read(39012, <<X:16, Y:16>>) ->
	{ok, [X, Y]};

%% -----------------------------------------------------------------
%% 39011 交战旗或魔核
%% -----------------------------------------------------------------
read(39011, <<FeatsType:8, Color:8, Point:8>>) ->
	{ok, [FeatsType, Color, Point]};

%% -----------------------------------------------------------------
%% 39016 占据据点
%% -----------------------------------------------------------------
read(39016, <<Type:8>>) ->
	{ok, [Type]};

%% -----------------------------------------------------------------
%% 39021 积分排行
%% -----------------------------------------------------------------
read(39021, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39023 氏族战开启时间广播(广播)
%% -----------------------------------------------------------------
read(39023, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39024 查询功勋
%% -----------------------------------------------------------------
read(39024, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39025 氏族战奖励
%% -----------------------------------------------------------------
read(39025, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39026 氏族战奖励	分配上一场空战信息及物品获取物品
%% -----------------------------------------------------------------
read(39026, <<GuildId:32>>) ->
	{ok, [GuildId]};

%% -----------------------------------------------------------------
%% 39027 氏族战奖励	物品分配物品
%% -----------------------------------------------------------------
read(39027, <<PlayerId:32, GoodsTypeId:32, Num:32>>) ->
	{ok, [PlayerId, GoodsTypeId, Num]};

%% -----------------------------------------------------------------
%% 39028 氏族战奖励	物品自动分配
%% -----------------------------------------------------------------
read(39028, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39029查询氏族战是否报名
%% -----------------------------------------------------------------
read(39029, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 39102 进入/离开神魔乱斗场景
%% -----------------------------------------------------------------
read(39102, <<Type:8>>) ->
	{ok, [Type]};
	
%% -----------------------------------------------------------------
%% 39108 吃掉一个铜币图标
%% -----------------------------------------------------------------
read(39108, <<Key:32>>) ->
	{ok, [Key]};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
read(_Cmd, _R) ->
    {error, no_match}.

%%%=========================================================================
%%% 组包函数
%%%=========================================================================
%% -----------------------------------------------------------------
%% 广播协议
%% -----------------------------------------------------------------
write(39000, [Type, String]) ->
%% 	io:format("39000\n"),
	{GNLen, GNBin} = lib_guild_inner:string_to_binary_and_len(String),
	BinData = <<Type:16, GNLen:16, GNBin/binary>>,
	{ok, pt:pack(39000, BinData)};

%% -----------------------------------------------------------------
%% 39001 查看已报名氏族
%% -----------------------------------------------------------------
write(39001, [Array]) ->
	Data = handle_apply_guilds(Array, []),
	Length = length(Data),
	BinData = tool:to_binary(Data),
	{ok, pt:pack(39001, <<Length:16, BinData/binary>>)};

%% -----------------------------------------------------------------
%% 39002 报名空岛神战
%% -----------------------------------------------------------------
write(39002, [Result]) ->
	{ok, pt:pack(39002, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39003 进入空岛神战
%% -----------------------------------------------------------------
write(39003, [Result]) ->
	{ok, pt:pack(39003, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39004 离开空岛神战
%% -----------------------------------------------------------------
write(39004, [Result]) ->
	{ok, pt:pack(39004, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39005 boss击杀广播
%% -----------------------------------------------------------------
write(39005, [BossTypeId]) ->
	{ok, pt:pack(39005, <<BossTypeId:8>>)};

%% -----------------------------------------------------------------
%% 39006 据点占领广播
%% -----------------------------------------------------------------
write(39006, [PointId, GuildName]) ->
	{GNLen, GNBin} = lib_guild_inner:string_to_binary_and_len(GuildName),
	{ok, pt:pack(39006, <<PointId:8, GNLen:16, GNBin/binary>>)};


%% -----------------------------------------------------------------
%% 39008 开旗
%% -----------------------------------------------------------------
write(39008, [Result]) ->
	{ok, pt:pack(39008, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39010 获得战旗或魔核
%% -----------------------------------------------------------------
write(39010, [PlayerId, Type, Color]) ->
	{ok, pt:pack(39010, <<PlayerId:32, Type:8, Color:8>>)};

%% -----------------------------------------------------------------
%% 39007 功勋广播 -- 1 战场信息(list) --0
%% -----------------------------------------------------------------
write(39007, [Type, Array]) ->
%% 	?DEBUG("39007, Type: ~p, Array: ~p", [Type, Array]),
	{Len, List} = handle_sky_info(0, Array, []),
	DataBin = tool:to_binary(List),
	{ok, pt:pack(39007, <<Type:8, Len:16, DataBin/binary>>)};

%% -----------------------------------------------------------------
%% 39009 结束神战
%% -----------------------------------------------------------------
write(39009, []) ->
	{ok, pt:pack(39009, <<>>)};

%% -----------------------------------------------------------------
%% 39011 交战旗或魔核
%% -----------------------------------------------------------------
write(39011, [Result]) ->
	{ok, pt:pack(39011, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39012 拾取战旗
%% -----------------------------------------------------------------
write(39012, [Result, X, Y, Color]) ->
	{ok, pt:pack(39012, <<Result:16, X:16, Y:16, Color:8>>)};

%% -----------------------------------------------------------------
%% 39013 广播掉落战旗或魔核
%% -----------------------------------------------------------------
write(39013, [Color, X, Y]) ->
	{ok, pt:pack(39013, <<Color:8, X:16, Y:16>>)};

%% -----------------------------------------------------------------
%% 39014 刷新战旗
%% -----------------------------------------------------------------
write(39014, [Color, X, Y]) ->
	{ok, pt:pack(39014, <<Color:8, X:16, Y:16>>)};

%% -----------------------------------------------------------------
%% 39015 战旗消失
%% -----------------------------------------------------------------
write(39015, [Color, Area, PlayerId]) ->
	{ok, pt:pack(39015, <<Color:8, Area:8, PlayerId:32>>)};
	
%% -----------------------------------------------------------------
%% 39016 占据据点
%% -----------------------------------------------------------------
write(39016, [Result]) ->
	{ok, pt:pack(39016, <<Result:16>>)};
%% -----------------------------------------------------------------
%% 39016 占据据点
%% -----------------------------------------------------------------
write(39017, []) ->
	{ok, pt:pack(39017, <<>>)};
%% -----------------------------------------------------------------
%% 39016 占据据点
%% -----------------------------------------------------------------
write(39018, []) ->
	{ok, pt:pack(39018, <<>>)};


%% -----------------------------------------------------------------
%% 39019 战场信息2
%% -----------------------------------------------------------------
write(39019, [BossOne, BossTwo, BossThree, PointL, LName, PointH, HName, OneColor, TwoColor, ThreeColor, RemainTime]) ->
%% 	?DEBUG("39019, BossOne:~p, BossTwo:~p, BossThree:~p, PointL:~p, PointH:~p, OneColor:~p, TwoColor:~p, ThreeColor:~p, RemainTime:~p",
%% 		   [BossOne, BossTwo, BossThree, PointL, PointH, OneColor, TwoColor, ThreeColor, RemainTime]),
	{LLen, LBin} = lib_guild_inner:string_to_binary_and_len(LName),
	{HLen, HBin} = lib_guild_inner:string_to_binary_and_len(HName),
	{ok, pt:pack(39019, <<BossOne:8, BossTwo:8, BossThree:8, PointL:8, LLen:16, LBin/binary, PointH:8, HLen:16, HBin/binary, OneColor:8, TwoColor:8, ThreeColor:8,RemainTime:32>>)};

%% -----------------------------------------------------------------
%% 39020 据点刷新
%% -----------------------------------------------------------------
write(39020, []) ->
	{ok, pt:pack(39020, <<>>)};

%% -----------------------------------------------------------------
%% 39021 积分排行
%% -----------------------------------------------------------------
write(39021, [Result]) ->
	{OneList, TwoList, ThreeList} = Result,
%% 	io:format("~p\n", [Result]),
	{OneLen, OneBin} = handle_one_list(OneList), 
	{TwoLen, TwoBin} = handle_two_list(TwoList), 
	{ThreeLen, ThreeBin} = handle_three_list(ThreeList), 
	{ok, pt:pack(39021, <<OneLen:16, OneBin/binary, TwoLen:16, TwoBin/binary, ThreeLen:16, ThreeBin/binary>>)};

%% -----------------------------------------------------------------
%% 39022 清理丢在场上的战旗(广播)
%% -----------------------------------------------------------------
write(39022, [X,Y,Type]) ->
	{ok, pt:pack(39022, <<X:16,Y:16,Type:8>>)};

%% -----------------------------------------------------------------
%% 39023 氏族战开启时间广播(广播)
%% -----------------------------------------------------------------
write(39023, [RemainTime]) ->
	{ok, pt:pack(39023, <<RemainTime:32>>)};

%% -----------------------------------------------------------------
%% 39024 查询功勋
%% -----------------------------------------------------------------
write(39024, [GFeat]) ->
	{ok, pt:pack(39024, <<GFeat:32>>)};

%% -----------------------------------------------------------------
%% 39025 氏族战奖励
%% -----------------------------------------------------------------
write(39025, [Result, Exp, Spirit]) ->
	ExpStr = util:term_to_string(Exp),
	ExpBin = tool:to_binary(ExpStr),	
	SpiritStr = util:term_to_string(Spirit),
	SpiritBin = tool:to_binary(SpiritStr),	
	ExpLen = byte_size(ExpBin),
	SpiritLen = byte_size(SpiritBin),
	{ok, pt:pack(39025, <<Result:16, ExpLen:16, ExpBin/binary, SpiritLen:16, SpiritBin/binary>>)};
	
%% -----------------------------------------------------------------
%% 39026 氏族战奖励	分配上一场空战信息及物品获取物品
%% -----------------------------------------------------------------
write(39026, [AwardGoods, FeatMember]) ->
	ALen = length(AwardGoods),
	ACoded = lists:map(fun(AElem) ->
							 {GoodsId, Num} = AElem,
							 <<GoodsId:32, Num:32>>
					 end, AwardGoods),
	ABin = tool:to_binary(ACoded),
	
	FLen = length(FeatMember),
	FCoded = lists:map(fun(FElem) ->
							   {PlayerId, PlayerName,Career, Sex, Feats} = FElem,
							   {PLen, PBin} = lib_guild_inner:string_to_binary_and_len(PlayerName),
							   <<PlayerId:32, PLen:16, PBin/binary, Career:8, Sex:8, Feats:32>>
					   end, FeatMember),
	FBin = tool:to_binary(FCoded),
	
	{ok, pt:pack(39026, <<FLen:16, FBin/binary, ALen:16, ABin/binary>>)};


%% -----------------------------------------------------------------
%% 39027 氏族战奖励	物品分配物品
%% -----------------------------------------------------------------
write(39027, [Result, GoodsTypeId, NewNum]) ->
	{ok, pt:pack(39027, <<Result:16, GoodsTypeId:32, NewNum:32>>)};

%% -----------------------------------------------------------------
%% 39028 氏族战奖励	物品自动分配
%% -----------------------------------------------------------------
write(39028, [Result]) ->
	{ok, pt:pack(39028, <<Result:16>>)};

%% -----------------------------------------------------------------
%% 39029 通知报名
%% -----------------------------------------------------------------
write(39029,[Timestamp])->
	{ok, pt:pack(39029, <<Timestamp:32>>)};

%% -----------------------------------------------------------------
%% 39030 神岛空战进入通知
%% -----------------------------------------------------------------
write(39030,[])->
%%	?DEBUG("39030", []),
	{ok, pt:pack(39030, <<>>)};

%% -----------------------------------------------------------------
%% 39100 神魔乱斗场景广播
%% -----------------------------------------------------------------
write(39100, [Type, CastStr]) ->
%% 	?DEBUG("39100", []),
	StrBin = tool:to_binary(CastStr),	
	StrLen = byte_size(StrBin),
	{ok, pt:pack(39100, <<Type:8, StrLen:16, StrBin/binary>>)};

%% -----------------------------------------------------------------
%% 39101 神魔乱斗通知
%% -----------------------------------------------------------------
write(39101, [RTime]) ->
	{ok, pt:pack(39101, <<RTime:32>>)};

%% -----------------------------------------------------------------
%% 39103 最终获得冥王之灵
%% -----------------------------------------------------------------
write(39103, [RTime, PlutoNames]) ->
	{Len, Bin} = handle_pluto_pinfo([], PlutoNames),
%% 	?DEBUG("39103 RTime:~p,PlutoNames:~p, Len:~p", [RTime, PlutoNames, Len]),
	{ok, pt:pack(39103, <<RTime:32, Len:16, Bin/binary>>)};
	
%% -----------------------------------------------------------------
%% 39101 通知去掉冥王之灵的图标
%% -----------------------------------------------------------------
write(39104, []) ->
	{ok, pt:pack(39104, <<>>)};

%% -----------------------------------------------------------------
%% 39105 伤害列表
%% -----------------------------------------------------------------
write(39105, [AttSum, SubList]) ->
%% 	?DEBUG("ATTSUM is:~p", [AttSum]),
	{Len, SubBin} = handle_39105(SubList, []),
	{ok, pt:pack(39105, <<AttSum:32, Len:16, SubBin/binary>>)};
	
%% -----------------------------------------------------------------
%% 39106 在神魔乱斗场景被杀信息
%% -----------------------------------------------------------------
write(39106, [Name]) ->
	%%?DEBUG("ATTSUM is:~p", [Name]),
	NameBin = tool:to_binary(Name),
	Len = byte_size(NameBin),
	{ok, pt:pack(39106, <<Len:16, NameBin/binary>>)};

%% -----------------------------------------------------------------
%% 39107 神魔乱斗所有铜币坐标
%% -----------------------------------------------------------------
write(39107, [List]) ->
	{Len, ListBin} = handle_list_39107(List, [], 0),
%% 	?DEBUG("Len:~p", [Len]),
	{ok, pt:pack(39107, <<Len:16, ListBin/binary>>)};

%% -----------------------------------------------------------------
%% 39108 吃掉一个铜币图标
%% -----------------------------------------------------------------
write(39108, [Result, BCoin]) ->
%% 	?DEBUG("Result:~p, BCoin:~p", [Result, BCoin]),
	{ok, pt:pack(39108, <<Result:8, BCoin:32>>)};

%% -----------------------------------------------------------------
%% 39109 消失一个铜币广播
%% -----------------------------------------------------------------
write(39109, [Key, PlayerId]) ->
%% 	?DEBUG("Key:~p, PlayerId:~p", [Key, PlayerId]),
	{ok, pt:pack(39109, <<Key:32, PlayerId:32>>)};

%% -----------------------------------------------------------------
%% 39110 消失所有铜币广播
%% -----------------------------------------------------------------
write(39110, []) ->
%% 	?DEBUG("39110", []),
	{ok, pt:pack(39110, <<>>)};

%% -----------------------------------------------------------------
%% 39111 发送已领取的绑定铜数值 
%% -----------------------------------------------------------------
write(39111, [Type, BCoin]) ->
	{ok, pt:pack(39111, <<Type:8, BCoin:32>>)};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
write(Cmd, _R) ->
?INFO_MSG("~s_errorcmd_[~p] ",[misc:time_format(yg_timer:now()), Cmd]),
    {ok, pt:pack(0, <<>>)}.

%%
%% Local Functions
%%
handle_list_39107([], Result, Num) ->
	{Num, tool:to_binary((Result))};
handle_list_39107([Elem|List], Result, Num) ->
	{Key, IsGot, {X, Y}} = Elem,
	case IsGot of
		0 ->%%没有被领取的绑定铜
			ElemBin = <<Key:32, X:32, Y:32>>,
			handle_list_39107(List, [ElemBin|Result], Num+1);
		_ ->%%已经被领取过了的
			handle_list_39107(List, Result, Num)
	end.

			
%%排行榜打包
handle_one_list(OneList) ->
	Len = length(OneList),
	List = lists:map(fun(Elem) ->
							 {GuildName, GLv, JoinNum, FeatsL} = Elem,
							 {GLen, GBin} = lib_guild_inner:string_to_binary_and_len(GuildName),
							 <<GLen:16, GBin/binary, GLv:16, JoinNum:32, FeatsL:32>>
					 end, OneList),
	BinData = tool:to_binary(List),
	{Len, BinData}.
handle_two_list(TwoList) ->
	Len = length(TwoList),
	List = lists:map(fun(Elem) ->
							 {GuildName, GLv, FeatsAll, FeatsW} = Elem,
							 {GLen, GBin} = lib_guild_inner:string_to_binary_and_len(GuildName),
							 <<GLen:16, GBin/binary, GLv:16, FeatsAll:32, FeatsW:32>>
					 end, TwoList),
	BinData = tool:to_binary(List),
	{Len, BinData}.
handle_three_list(ThreeList) ->
	Len = length(ThreeList),
	List = lists:map(fun(Elem) ->
							 {PlayerName, Career, PLv, KillFoe, DieC, GFlags, MNuts, Feat} = Elem,
							 {PLen, PBin} = lib_guild_inner:string_to_binary_and_len(PlayerName),
							 <<PLen:16, PBin/binary, Career:8, PLv:32, KillFoe:32, DieC:32, GFlags:32, MNuts:32, Feat:32>>
					 end, ThreeList),
	BinData = tool:to_binary(List),
	{Len, BinData}.

%%申请
handle_apply_guilds([], Result) ->
	Result;
handle_apply_guilds([Elem|Array], Result) ->
	{GuildId, GLv, GMems, GuildName, ChiefName} = Elem,
	{GNLen, GNBin} = lib_guild_inner:string_to_binary_and_len(GuildName),
	{CNLen, CNBin} = lib_guild_inner:string_to_binary_and_len(ChiefName),
	ElemBin = <<GuildId:32, GLv:8, GMems:16, GNLen:16, GNBin/binary, CNLen:16, CNBin/binary>>,
	handle_apply_guilds(Array, [ElemBin|Result]).

handle_sky_info(Num, [], Result) ->
	{Num, Result};
handle_sky_info(Num, [Elem|Array], Result) ->
	{Type, Feats, Name} = Elem,
	{NLen, NBin} = lib_guild_inner:string_to_binary_and_len(Name),
	Bin = <<Type:8, Feats:16, NLen:16, NBin/binary>>,
	handle_sky_info(Num+1, Array, [Bin|Result]).
	
handle_pluto_pinfo(Result, []) ->
	{length(Result), tool:to_binary(Result)};
handle_pluto_pinfo(Result, [Elem|PlutoNames]) ->
	{NLen, NBin} = lib_guild_inner:string_to_binary_and_len(Elem),
	Bin = <<NLen:16, NBin/binary>>,
	handle_pluto_pinfo([Bin|Result], PlutoNames).
handle_39105([], Result) ->
	{length(Result), tool:to_binary(Result)};
handle_39105([{_Id, Name, Att}|SubList], Result) ->
	{NLen, NBin} = lib_guild_inner:string_to_binary_and_len(Name),
%% 	io:format("Id:~p, Att:~p", [_Id, Att]),
	Bin = <<Att:32, NLen:16, NBin/binary>>,
	handle_39105(SubList, [Bin|Result]).